package com.zut.controller;

import com.github.pagehelper.PageInfo;
import com.zut.domain.Reply;
import com.zut.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/reply")
public class ReplyController {

    @Autowired
    private ReplyService replyService;

    /*得到所有post*/
    @GetMapping("reply/{pageNum}/{pageSize}")
    public Result getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Reply> replies = replyService.getAllReply(pageNum, pageSize);
        Integer code = replies != null ? Code.GET_OK : Code.GET_ERR;
        String msg = replies != null ? "" : "数据查询失败，请重试！";
        return new Result(code, replies, msg);
    }

    /*根据postId得到所有回复*/
    @GetMapping("reply/{pageNum}/{pageSize}/{id}")
    public Result getAllByPostId(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable String id) {
        PageInfo<Reply> replies = replyService.getAllByPostId(pageNum, pageSize, id);
        Integer code = replies != null ? Code.GET_OK : Code.GET_ERR;
        String msg = replies != null ? "" : "数据查询失败，请重试！";
        return new Result(code, replies, msg);
    }

    /*根据主贴id获得回复信息*/
    @GetMapping("/{pageNum}/{pageSize}/{postId}/{userId}")
    public Result getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable String postId, @PathVariable String userId) {
        PageInfo<Map<String, String>> replies = replyService.getAll(pageNum, pageSize, postId, userId);
        Integer code = replies != null ? Code.GET_OK : Code.GET_ERR;
        String msg = replies != null ? "" : "数据查询失败，请重试！";
        return new Result(code, replies, msg);
    }

    /*发布回复信息*/
    @PostMapping
    public Result insert(@RequestBody Reply reply) {
        boolean flag = replyService.insert(reply);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    /*根据回复id删除回复信息*/
    @DeleteMapping(value = "/delete/{id}")
    public Result deleteById(@PathVariable String id) {
        boolean flag = replyService.deleteById(id);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /*更新回复内容*/
    @PutMapping
    public Result Update(@RequestBody Reply reply) {
        boolean flag = replyService.update(reply);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /*点赞+1*/
    @PostMapping("likeOn/{replyId}/{userId}")
    public Result likeOn(@PathVariable String replyId, @PathVariable String userId) {
        boolean flag = replyService.LikeOn(replyId, userId);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /*点赞-1*/
    @PostMapping("likeDown/{replyId}/{userId}")
    public Result likeDown(@PathVariable String replyId, @PathVariable String userId) {
        boolean flag = replyService.LikeDown(replyId, userId);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /*根据用户id获取该用户所有的评论*/
    @GetMapping("user/{id}")
    public Result getAllByUserID(@PathVariable String id) {
        List<Map<String, String>> replies = replyService.getAllByUserId(id);
        Integer code = replies != null ? Code.GET_OK : Code.GET_ERR;
        String msg = replies != null ? "" : "数据查询失败，请重试！";
        return new Result(code, replies, msg);
    }
}
